package com.nerve.secondhandcar.controller.reception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ko.core.web.BaseController;
import org.ko.core.websupport.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nerve.secondhandcar.controller.model.ResaleApplyModel;
import com.nerve.secondhandcar.controller.model.reception.IndexModel;
import com.nerve.secondhandcar.domain.ResaleApply;
import com.nerve.secondhandcar.service.BrandService;
import com.nerve.secondhandcar.service.reception.IndexService;

/**
 * @project: secondHandCarPlatform
 * @file: IndexController.java
 * @package: com.nerve.secondhandcar.controller.reception
 * @description:
 *
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: 2014-10-22
 * @change log:
 */
@Controller
public class IndexController extends BaseController{

	private static final String RESALE = "form-sell";
	@Autowired
	private IndexService service;
	@Autowired
	private BrandService brandService;
	
	@Override
	protected String getTemplatePath() {
		return "car/reception/";
	}

	/**
	 * 首页
	 * @method name: index
	 * @return type: String
	 *	@param req
	 *	@return
	 *	@throws Exception
	 */
	@RequestMapping(value="index", method=RequestMethod.GET)
	public String index(HttpServletRequest req, ModelMap map, IndexModel model)
		throws Exception{
		service.index(model);
		map.addAttribute("model", model);
		map.addAttribute("brandList", brandService.findForSelect());
		
		return view("index");
	}
	
	/**
	 * 汽车列表
	 * @method name: list
	 * @return type: String
	 *	@param req
	 *	@param map
	 *	@param model
	 *	@return
	 *	@throws Exception
	 */
	@RequestMapping(value="list")
	public String list(HttpServletRequest req, ModelMap map, IndexModel model)
		throws Exception{
		service.list(model);
		map.addAttribute(PAGINATION, model);
		
		return view(LIST);
	}
	
	/**
	 * 查看车辆详细信息
	 * @method name: detail
	 * @return type: String
	 *	@param req
	 *	@param map
	 *	@param model
	 *	@return
	 *	@throws Exception
	 */
	@RequestMapping(value="detail", method=RequestMethod.GET)
	public String detail(HttpServletRequest req, ModelMap map, IndexModel model) throws Exception{
		service.detail(model);
		map.addAttribute("car", model.getCar());
		
		return view("detail");
	}
	
	/**
	 * 创建了转售申请
	 * @method name: create
	 * @return type: String
	 *	@param request
	 *	@param response
	 *	@param model
	 *	@return
	 *	@throws Exception
	 */	
	@RequestMapping(value="resale", method = RequestMethod.GET)
	public String resale(){
		return view(RESALE);
	}
	
	@Log(message="创建了转售申请", category="RESALEAPPLY")
	@RequestMapping(value="resale", method = RequestMethod.POST)
	public String create(HttpServletRequest request, HttpServletResponse response,ResaleApplyModel model, RedirectAttributes redirt){
		try {
			service.create(model);
		
			redirt.addFlashAttribute("result", "您的申请已经提交成功，请保存手机畅通，我们会尽快与您联系");
			return redirect(model.getBean().getCategory()==ResaleApply.RESALE?"resale.do":"assessment.do");
		} catch (Exception e) {
			e.printStackTrace();
			redirt.addFlashAttribute("result", e.getMessage());
			return redirect("resale.do");
		}
	}
	
	/**
	 * 我要估价，跟转售一样的
	 * @method name: assessment
	 * @return type: String
	 *	@return
	 */
	@RequestMapping(value="assessment", method = RequestMethod.GET)
	public String assessment(HttpServletRequest req, ModelMap map){
		map.addAttribute("type", ResaleApply.ASSESSMENT);
		map.addAttribute("pageTitle", "我要估价");
		return view("form-assessment");
	}

}